package cn.sowz.test;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

public class Demo06 {
    private ProcessEngine processEngine;
    @Before
    public void before() throws Exception {
        // 启动引擎
        this.processEngine = ProcessEngines.getDefaultProcessEngine();
    }

    // 查询流程定义对象
    @Test
    public void testDefinitionQuery(){
        // 流程定义对象---当部署成功之后就存在的，跟资源操作有关--repositoryService
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        // 查询
        List<ProcessDefinition> leaveProcess = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("leaveProcess")
                // .latestVersion() // 最后版本
                // .orderByProcessDefinitionVersion() // 根据版本号排序
                .list();
        for (ProcessDefinition definition : leaveProcess) {
            System.out.println("流程定义ID: " + definition.getId());
            System.out.println("流程定义名称: " + definition.getName());
            System.out.println("流程定义key: " + definition.getKey());
            System.out.println("流程定义版本: " + definition.getVersion());
            System.out.println("流程部署ID: " + definition.getDeploymentId());
            System.out.println("=========================");
        }
    }

    // 下载流程图文件与流程图片
    @Test
    public void testDownloadResource() throws Exception{
        // 获取仓库服务
        RepositoryService repositoryService = this.processEngine.getRepositoryService();

        // 下载资源需要2个参数：1>部署id 2>资源名称 ---2个参数都可以流程定义对象中找到ProcessDefinition
        // 所以：下载资源前先查询流程定义对象：ProcessDefinition
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("leaveProcess")
                .latestVersion()
                // .orderByProcessDefinitionVersion()
                // .desc()
                .singleResult();

        // 流程文件
        InputStream bpmnInput = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
        // 图片
        InputStream pngInput = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getDiagramResourceName());

        FileOutputStream bpmnOutput = new FileOutputStream("/home/ke666/my_codes/leave/leave.bpmn");
        FileOutputStream pngOutput = new FileOutputStream("/home/ke666/my_codes/leave/leave.png");

        IOUtils.copy(bpmnInput,bpmnOutput);
        IOUtils.copy(pngInput,pngOutput);
    }

    // 流程定义删除
    @Test
    public void testDeleteDeploy(){
        // 获取仓库服务
        RepositoryService repositoryService = this.processEngine.getRepositoryService();

        // 删除部署对象
        // repositoryService.deleteDeployment("10001");
        // 如果流程在启动中，是无法直接删除deployment对象，如  果需要强制删除，可以使用级联删除
        repositoryService.deleteDeployment("1",true);
    }
}
